clear all
set more off
capture log close
log using "$output\figure8.log", replace

use "$data/acsmergeddata.dta", clear

gen pooradults=(age>=18)*poor*(perwt+spouseperwt)
egen totpooradults=sum(pooradults)

gen nolongerpooradults=(age>=18)*poor*(poorctc21==0)*(perwt+spouseperwt)
egen totnolongerpooradults=sum(nolongerpooradults)

gen stillpooradults=(age>=18)*poor*(poorctc21==1)*(perwt+spouseperwt)
egen totstillpooradults=sum(stillpooradults)

**************************************************
* KIDS 
**************************************************

drop stillpoor totstillpoor 

gen poorkids=poor*(newnumkidswt17)
egen totpoorkids=sum(poorkids)
di totpoorkids

gen stillpoor=poor*(poorctc21==1)*(newnumkidswt17)
egen totstillpoor=sum(stillpoor)

gen pop=(newnumkidswt17)
egen totkids=sum(pop)

di totkids
di totpoorkids
di totstillpoor

di totpoorkids/totkids
di (totstillpoor)/totkids
drop pop


tab poor,m

** VARIOUS ELASTICITIES FOR UNMARRIED MOTHERS

forvalues elas=0(1)20{
preserve

* row 1
gen double group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*(`elas'/10)*group
egen totgroup1a=sum(group1)
gen double group2=newnumkidswt17*avgtax*(`elas'/10)*group*poor
egen totgroup2a=sum(group2)

gen double group3=newnumkidswt17*avgtax*(`elas'/10)*group*(poor==0)
egen totgroup3a=sum(group3)

gen double group4=newnumkidswt17*avgtax*(`elas'/10)*group*(poor==0)*poorctc21nother
egen totgroup4a=sum(group4)

gen double group5=newnumkidswt17*avgtax*(`elas'/10)*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5a=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'a=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'a/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 2
gen double group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.2*group
egen totgroup1b=sum(group1)

gen double group2=newnumkidswt17*avgtax*.2*group*poor
egen totgroup2b=sum(group2)

gen double group3=newnumkidswt17*avgtax*.2*group*(poor==0)
egen totgroup3b=sum(group3)

gen double group4=newnumkidswt17*avgtax*.2*group*(poor==0)*poorctc21nother
egen totgroup4b=sum(group4)

gen double group5=newnumkidswt17*avgtax*.2*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5b=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'b=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'b/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 3
gen double group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.2*group
egen totgroup1c=sum(group1)

gen double group2=newnumkidswt17*avgtax*.2*group*poor
egen totgroup2c=sum(group2)

gen double group3=newnumkidswt17*avgtax*.2*group*(poor==0)
egen totgroup3c=sum(group3)

gen double group4=newnumkidswt17*avgtax*.2*group*(poor==0)*poorctc21nother
egen totgroup4c=sum(group4)

gen double group5=newnumkidswt17*avgtax*.2*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5c=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'c=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'c/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 4
gen double group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.05*group
egen totgroup1d=sum(group1)

gen double group2=newnumkidswt17*avgtax*.05*group*poor
egen totgroup2d=sum(group2)

gen double group3=newnumkidswt17*avgtax*.05*group*(poor==0)
egen totgroup3d=sum(group3)

gen double group4=newnumkidswt17*avgtax*.05*group*(poor==0)*poorctc21notspouse
egen totgroup4d=sum(group4)

gen double group5=newnumkidswt17*avgtax*.05*group*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5d=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'd=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'd/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)

gen double group1=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.05
egen totgroup1e=sum(group1)

gen double group2=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.05*poor
egen totgroup2e=sum(group2)

gen double group3=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.05*(poor==0)
egen totgroup3e=sum(group3)

gen double group4=(newnumkidswt17*person1)*avgtax*.05*(poor==0)*poorctc21nother+(newnumkidswt17*person2)*avgtax*.05*(poor==0)*poorctc21notspouse

egen totgroup4e=sum(group4)

gen double group5=(newnumkidswt17*person1)*avgtax*.05*(poor==1)*(poorctc21==0)*poorctc21nother+newnumkidswt17*person2*avgtax*.05*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5e=sum(group5)



gen pov1=(poor*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov2=(poorctc21*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov3=(poorctc21*(person1*newnumkidswt17+person2*newnumkidswt17)+group4+group5)

egen pop=sum(newnumkidswt17*person1+newnumkidswt17*person2)
forvalues i=1/3{
	egen povnum`i'e=sum(pov`i')
	gen povrate`i'=povnum`i'e/pop
	}


drop pov1 pov2 pov3 povrate* pop group5 group*

forvalues i=1/5{
	di totgroup`i'a+totgroup`i'b+totgroup`i'c+totgroup`i'd+totgroup`i'e
}

di totkids
di totpoorkids
di totstillpoor
di (totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
di totpoorkids/totkids
di totstillpoor/totkids
di (totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)/totkids
di "pov % decrease="
di (totpoorkids-(totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totpoorkids


local netkidpovrate_e`elas'=(totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
local kidpovchange_e`elas'=(totpoorkids-(totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totpoorkids


di `netkidpovrate_e`elas'' 
di `kidpovchange_e`elas''
restore

}

gen elasticity=.
gen netkidpovrate1=.
gen kidpovchange1=.

forvalues elas=0(1)20{
	replace elasticity=`elas' if _n==`elas'+1
	replace netkidpovrate1=`netkidpovrate_e`elas''/1000000 if _n==`elas'+1
	replace kidpovchange1=`kidpovchange_e`elas'' if _n==`elas'+1
}


* EVERYONE ELSE GETS 0.25

forvalues elas=0(1)20{
preserve

* row 1
gen double group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*(`elas'/10)*group
egen totgroup1a=sum(group1)
gen double group2=newnumkidswt17*avgtax*(`elas'/10)*group*poor
egen totgroup2a=sum(group2)

gen double group3=newnumkidswt17*avgtax*(`elas'/10)*group*(poor==0)
egen totgroup3a=sum(group3)

gen double group4=newnumkidswt17*avgtax*(`elas'/10)*group*(poor==0)*poorctc21nother
egen totgroup4a=sum(group4)

gen double group5=newnumkidswt17*avgtax*(`elas'/10)*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5a=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'a=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'a/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 2
gen double group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.25*group
egen totgroup1b=sum(group1)

gen double group2=newnumkidswt17*avgtax*.25*group*poor
egen totgroup2b=sum(group2)

gen double group3=newnumkidswt17*avgtax*.25*group*(poor==0)
egen totgroup3b=sum(group3)

gen double group4=newnumkidswt17*avgtax*.25*group*(poor==0)*poorctc21nother
egen totgroup4b=sum(group4)

gen double group5=newnumkidswt17*avgtax*.25*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5b=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'b=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'b/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 3
gen double group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.25*group
egen totgroup1c=sum(group1)

gen double group2=newnumkidswt17*avgtax*.25*group*poor
egen totgroup2c=sum(group2)

gen double group3=newnumkidswt17*avgtax*.25*group*(poor==0)
egen totgroup3c=sum(group3)

gen double group4=newnumkidswt17*avgtax*.25*group*(poor==0)*poorctc21nother
egen totgroup4c=sum(group4)

gen double group5=newnumkidswt17*avgtax*.25*group*(poor==1)*(poorctc21==0)*poorctc21nother
egen totgroup5c=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'c=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'c/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 4
gen double group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.25*group
egen totgroup1d=sum(group1)

gen double group2=newnumkidswt17*avgtax*.25*group*poor
egen totgroup2d=sum(group2)

gen double group3=newnumkidswt17*avgtax*.25*group*(poor==0)
egen totgroup3d=sum(group3)

gen double group4=newnumkidswt17*avgtax*.25*group*(poor==0)*poorctc21notspouse
egen totgroup4d=sum(group4)

gen double group5=newnumkidswt17*avgtax*.25*group*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5d=sum(group5)

gen pov1=(poor*group)/(group)
gen pov2=(poor*group*(poorctc21==1))/(group)
gen pov3=(poor*group*(poorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'd=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'd/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)

gen double group1=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25
egen totgroup1e=sum(group1)

gen double group2=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25*poor
egen totgroup2e=sum(group2)

gen double group3=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25*(poor==0)
egen totgroup3e=sum(group3)

gen double group4=(newnumkidswt17*person1)*avgtax*.25*(poor==0)*poorctc21nother+(newnumkidswt17*person2)*avgtax*.25*(poor==0)*poorctc21notspouse
*gen double group4=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25*(poor==0)*poorctc21nother
egen totgroup4e=sum(group4)

gen double group5=(newnumkidswt17*person1)*avgtax*.25*(poor==1)*(poorctc21==0)*poorctc21nother+newnumkidswt17*person2*avgtax*.25*(poor==1)*(poorctc21==0)*poorctc21notspouse
egen totgroup5e=sum(group5)



gen pov1=(poor*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov2=(poorctc21*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov3=(poorctc21*(person1*newnumkidswt17+person2*newnumkidswt17)+group4+group5)

egen pop=sum(newnumkidswt17*person1+newnumkidswt17*person2)
forvalues i=1/3{
	egen povnum`i'e=sum(pov`i')
	gen povrate`i'=povnum`i'e/pop
	}


drop pov1 pov2 pov3 povrate* pop group5 group*

forvalues i=1/5{
	di totgroup`i'a+totgroup`i'b+totgroup`i'c+totgroup`i'd+totgroup`i'e
}

di totkids
di totpoorkids
di totstillpoor
di (totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
di totpoorkids/totkids
di totstillpoor/totkids
di (totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)/totkids
di "pov % decrease="
di (totpoorkids-(totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totpoorkids


local netkidpovrate_e`elas'=(totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
local kidpovchange_e`elas'=(totpoorkids-(totstillpoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totpoorkids


di `netkidpovrate_e`elas'' 
di `kidpovchange_e`elas''
restore

}

gen netkidpovrate2=.
gen kidpovchange2=.
replace elasticity=elasticity/10

forvalues elas=0(1)20{
	
	replace netkidpovrate2=`netkidpovrate_e`elas''/1000000 if _n==`elas'+1
	replace kidpovchange2=`kidpovchange_e`elas'' if _n==`elas'+1
}

**************************************************
* DEEP POVERTY 
**************************************************

gen deeppoorkids=deeppoor*(newnumkidswt17)
egen totdeeppoorkids=sum(deeppoorkids)
di totdeeppoorkids

gen stilldeeppoor=deeppoor*(deeppoorctc21==1)*(newnumkidswt17)
egen totstilldeeppoor=sum(stilldeeppoor)

di totkids
di totdeeppoorkids
di totstilldeeppoor

di totdeeppoorkids/totkids
di (totstilldeeppoor)/totkids

***************************************************
** VARIOUS ELASTICITIES FOR UNMARRIED MOTHERS

forvalues elas=0(1)20{
preserve

* row 1
gen double group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*(`elas'/10)*group
egen totgroup1a=sum(group1)
gen double group2=newnumkidswt17*avgtax*(`elas'/10)*group*deeppoor
egen totgroup2a=sum(group2)

gen double group3=newnumkidswt17*avgtax*(`elas'/10)*group*(deeppoor==0)
egen totgroup3a=sum(group3)

gen double group4=newnumkidswt17*avgtax*(`elas'/10)*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4a=sum(group4)

gen double group5=newnumkidswt17*avgtax*(`elas'/10)*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5a=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'a=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'a/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 2
gen double group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.2*group
egen totgroup1b=sum(group1)

gen double group2=newnumkidswt17*avgtax*.2*group*deeppoor
egen totgroup2b=sum(group2)

gen double group3=newnumkidswt17*avgtax*.2*group*(deeppoor==0)
egen totgroup3b=sum(group3)

gen double group4=newnumkidswt17*avgtax*.2*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4b=sum(group4)

gen double group5=newnumkidswt17*avgtax*.2*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5b=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'b=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'b/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 3
gen double group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.2*group
egen totgroup1c=sum(group1)

gen double group2=newnumkidswt17*avgtax*.2*group*deeppoor
egen totgroup2c=sum(group2)

gen double group3=newnumkidswt17*avgtax*.2*group*(deeppoor==0)
egen totgroup3c=sum(group3)

gen double group4=newnumkidswt17*avgtax*.2*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4c=sum(group4)

gen double group5=newnumkidswt17*avgtax*.2*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5c=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'c=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'c/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 4
gen double group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.05*group
egen totgroup1d=sum(group1)

gen double group2=newnumkidswt17*avgtax*.05*group*deeppoor
egen totgroup2d=sum(group2)

gen double group3=newnumkidswt17*avgtax*.05*group*(deeppoor==0)
egen totgroup3d=sum(group3)

gen double group4=newnumkidswt17*avgtax*.05*group*(deeppoor==0)*deeppoorctc21notspouse
egen totgroup4d=sum(group4)

gen double group5=newnumkidswt17*avgtax*.05*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21notspouse
egen totgroup5d=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'd=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'd/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)

gen double group1=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.05
egen totgroup1e=sum(group1)

gen double group2=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.05*deeppoor
egen totgroup2e=sum(group2)

gen double group3=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.05*(deeppoor==0)
egen totgroup3e=sum(group3)

gen double group4=(newnumkidswt17*person1)*avgtax*.05*(deeppoor==0)*deeppoorctc21nother+(newnumkidswt17*person2)*avgtax*.05*(deeppoor==0)*deeppoorctc21notspouse
*gen double group4=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.05*(deeppoor==0)*deeppoorctc21nother
egen totgroup4e=sum(group4)

gen double group5=(newnumkidswt17*person1)*avgtax*.05*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother+newnumkidswt17*person2*avgtax*.05*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21notspouse
egen totgroup5e=sum(group5)



gen pov1=(deeppoor*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov2=(deeppoorctc21*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov3=(deeppoorctc21*(person1*newnumkidswt17+person2*newnumkidswt17)+group4+group5)

egen pop=sum(newnumkidswt17*person1+newnumkidswt17*person2)
forvalues i=1/3{
	egen povnum`i'e=sum(pov`i')
	gen povrate`i'=povnum`i'e/pop
	}


drop pov1 pov2 pov3 povrate* pop group5 group*

forvalues i=1/5{
	di totgroup`i'a+totgroup`i'b+totgroup`i'c+totgroup`i'd+totgroup`i'e
}

di totkids
di totdeeppoorkids
di totstilldeeppoor
di (totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
di totdeeppoorkids/totkids
di totstilldeeppoor/totkids
di (totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)/totkids
di "pov % decrease="
di (totdeeppoorkids-(totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totdeeppoorkids


local netkidpovrate_e`elas'=(totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
local kidpovchange_e`elas'=(totdeeppoorkids-(totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totdeeppoorkids


di `netkidpovrate_e`elas'' 
di `kidpovchange_e`elas''
restore

}

gen netkiddeeppovrate1=.
gen kiddeeppovchange1=.

forvalues elas=0(1)20{
	replace netkiddeeppovrate1=`netkidpovrate_e`elas''/1000000 if _n==`elas'+1
	replace kiddeeppovchange1=`kidpovchange_e`elas'' if _n==`elas'+1
}


* EVERYONE ELSE GETS 0.25

forvalues elas=0(1)20{
preserve

* row 1
gen double group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*(`elas'/10)*group
egen totgroup1a=sum(group1)
gen double group2=newnumkidswt17*avgtax*(`elas'/10)*group*deeppoor
egen totgroup2a=sum(group2)

gen double group3=newnumkidswt17*avgtax*(`elas'/10)*group*(deeppoor==0)
egen totgroup3a=sum(group3)

gen double group4=newnumkidswt17*avgtax*(`elas'/10)*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4a=sum(group4)

gen double group5=newnumkidswt17*avgtax*(`elas'/10)*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5a=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'a=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'a/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 2
gen double group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.25*group
egen totgroup1b=sum(group1)

gen double group2=newnumkidswt17*avgtax*.25*group*deeppoor
egen totgroup2b=sum(group2)

gen double group3=newnumkidswt17*avgtax*.25*group*(deeppoor==0)
egen totgroup3b=sum(group3)

gen double group4=newnumkidswt17*avgtax*.25*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4b=sum(group4)

gen double group5=newnumkidswt17*avgtax*.25*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5b=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'b=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'b/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 3
gen double group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.25*group
egen totgroup1c=sum(group1)

gen double group2=newnumkidswt17*avgtax*.25*group*deeppoor
egen totgroup2c=sum(group2)

gen double group3=newnumkidswt17*avgtax*.25*group*(deeppoor==0)
egen totgroup3c=sum(group3)

gen double group4=newnumkidswt17*avgtax*.25*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4c=sum(group4)

gen double group5=newnumkidswt17*avgtax*.25*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5c=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'c=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'c/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

* row 4
gen double group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.25*group
egen totgroup1d=sum(group1)

gen double group2=newnumkidswt17*avgtax*.25*group*deeppoor
egen totgroup2d=sum(group2)

gen double group3=newnumkidswt17*avgtax*.25*group*(deeppoor==0)
egen totgroup3d=sum(group3)

gen double group4=newnumkidswt17*avgtax*.25*group*(deeppoor==0)*deeppoorctc21notspouse
egen totgroup4d=sum(group4)

gen double group5=newnumkidswt17*avgtax*.25*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21notspouse
egen totgroup5d=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'd=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'd/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)

gen double group1=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25
egen totgroup1e=sum(group1)

gen double group2=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25*deeppoor
egen totgroup2e=sum(group2)

gen double group3=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25*(deeppoor==0)
egen totgroup3e=sum(group3)

gen double group4=(newnumkidswt17*person1)*avgtax*.25*(deeppoor==0)*deeppoorctc21nother+(newnumkidswt17*person2)*avgtax*.25*(deeppoor==0)*deeppoorctc21notspouse
*gen double group4=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25*(deeppoor==0)*deeppoorctc21nother
egen totgroup4e=sum(group4)

gen double group5=(newnumkidswt17*person1)*avgtax*.25*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother+newnumkidswt17*person2*avgtax*.25*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21notspouse
egen totgroup5e=sum(group5)



gen pov1=(deeppoor*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov2=(deeppoorctc21*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov3=(deeppoorctc21*(person1*newnumkidswt17+person2*newnumkidswt17)+group4+group5)

egen pop=sum(newnumkidswt17*person1+newnumkidswt17*person2)
forvalues i=1/3{
	egen povnum`i'e=sum(pov`i')
	gen povrate`i'=povnum`i'e/pop
	}


drop pov1 pov2 pov3 povrate* pop group5 group*

forvalues i=1/5{
	di totgroup`i'a+totgroup`i'b+totgroup`i'c+totgroup`i'd+totgroup`i'e
}

di totkids
di totdeeppoorkids
di totstilldeeppoor
di (totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
di totdeeppoorkids/totkids
di totstilldeeppoor/totkids
di (totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)/totkids
di "pov % decrease="
di (totdeeppoorkids-(totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totdeeppoorkids


local netkidpovrate_e`elas'=(totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
local kidpovchange_e`elas'=(totdeeppoorkids-(totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totdeeppoorkids


di `netkidpovrate_e`elas'' 
di `kidpovchange_e`elas''
restore

}

gen netkiddeeppovrate2=.
gen kiddeeppovchange2=.

forvalues elas=0(1)20{
	
	replace netkiddeeppovrate2=`netkidpovrate_e`elas''/1000000 if _n==`elas'+1
	replace kiddeeppovchange2=`kidpovchange_e`elas'' if _n==`elas'+1
}

foreach var in netkidpovrate1 netkidpovrate2 {
	replace `var'=11.177-`var'
	sum `var'
	}

foreach var in netkiddeeppovrate1 netkiddeeppovrate2 {
	replace `var'=3.11-`var'
	sum `var'
	}
	
keep elasticity netkid*povrate* kid*povchange*
duplicates drop	
sort elasticity

*** add in static estimate
expand 2 in l
replace elasticity=0 if _n==23
gen static=.
replace static=3.45 if _n==23

replace elasticity =.75 if _n==22 & elasticity==.
replace kidpovchange2=(kidpovchange2[_n-13]+kidpovchange2[_n-14])/2 if elasticity==.75
replace kiddeeppovchange2=(kiddeeppovchange2[_n-13]+kiddeeppovchange2[_n-14])/2 if elasticity==.75


twoway line netkidpovrate1 elasticity, c(l) pstyle(p1) || line netkidpovrate2 elasticity, c(l) pstyle(p2) lp(.) ytitle("Millions of Children Pulled Out of Poverty") graphregion(color(white)) bgcolor(white) xtitle("Elasticity for Unmarried EITC Mothers" "{bf:Panel A: Child Poverty}") xlabel(0(.4)2) || line kidpovchange1 elasticity, c(l) pstyle(p1) yaxis(2) || line kidpovchange2 elasticity, c(l) yaxis(2) pstyle(p2) lp(-) ytitle("Reduction in Child Poverty", axis(2)) || scatter kidpovchange1 elasticity if (elasticity>.39 & elasticity<.41)  , pstyle(p1) m(S) yaxis(2) || scatter kidpovchange2 elasticity if (elasticity>.74 & elasticity<.76) , pstyle(p2) m(S) yaxis(2)  || scatter static elasticity if _n==23 , pstyle(p1) m(Oh) msize(large) ylabel(.365 " " .3 "30%" .2 "20%" .1 "10%"  0 "0%" , axis(2)) ylabel(0(1)4.08, axis(1)) leg(order(1 "0.2 Elasticity for Other Moms, 0.05 for All Dads" 2 "0.25 Elasticity for Other Moms and All Dads" 3 "Static Estimate" 6 "My Preferred Estimate" 7 "Estimate from Higher Elasticities in Section 7.4" ) c(1) pos(6) ring(0)) 
graph export "$output/figure8a.png", replace

replace static=1.62 if _n==23

twoway line netkiddeeppovrate1 elasticity, c(l) pstyle(p1) || line netkiddeeppovrate2 elasticity, c(l) pstyle(p2) lp(-) ytitle("Millions of Children Pulled Out of Deep Poverty") graphregion(color(white)) bgcolor(white) xtitle("Elasticity for Unmarried EITC Mothers" "{bf:Panel B: Deep Child Poverty}") xlabel(0(.4)2) || line kiddeeppovchange1 elasticity, c(l) pstyle(p1) yaxis(2) || line kiddeeppovchange2 elasticity, c(l) yaxis(2) pstyle(p2) lp(-) ytitle("Reduction in Deep Child Poverty", axis(2)) || scatter kiddeeppovchange1 elasticity if elasticity>.39 & elasticity<.41 , pstyle(p1) m(S) yaxis(2) ylabel(.547 " " .5 "50%" .4 "40%" .3 "30%" .2 "20%" .1 "10%"  .0 "0%" , axis(2)) ylabel(0(.4)1.7, axis(1))  || scatter kiddeeppovchange2 elasticity if (elasticity>.74 & elasticity<.76) , pstyle(p2) m(S) yaxis(2) || scatter static elasticity if _n==23 , pstyle(p1) m(Oh) msize(large) leg(order(1 "0.2 Elasticity for Other Moms, 0.05 for All Dads" 2 "0.25 Elasticity for Other Moms and All Dads" 3 "Static Estimate" 6 "My Preferred Estimate" 7 "Estimate from Higher Elasticities in Section 7.4" ) c(1) pos(6) ring(0))
graph export "$output/figure8b.png", replace

log close